/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.tree;

import java.awt.*;
import java.util.ArrayList;

import javax.swing.*;

import com.pearreview.ui.*;
import com.pearreview.application.Messages;


public class ListControlPanel extends JPanel {

	private static final long serialVersionUID = 5932969672393831692L;
	private FormLabel headingWokspace;
	private FormLabel headingList;
	private JButton refreshBtn;
	private JButton catalogManagerBtn;
	private JButton createAspectBtn;
	private JButton createCategoryBtn;
	private JButton createCatalogBtn;
	private Action openCatalogManager;
	private Action addCategoryAction; 
	private Action addAspectAction;
	private Action createCatalogAction;
	private Action refreshAction;
	
	public ListControlPanel(
			Action openCatalogManager,
			Action addCategoryAction, 
			Action addAspectAction,
			Action saveAsCatalogAction,
			Action refreshAction) {
		
		super();
		
		this.openCatalogManager = openCatalogManager;
		this.addCategoryAction = addCategoryAction; 
		this.addAspectAction = addAspectAction;
		this.createCatalogAction = saveAsCatalogAction;
		this.refreshAction = refreshAction;
		
		initComponents();
	}

	private void initComponents() {

		headingWokspace = new FormLabel();
		headingList = new FormLabel();
		refreshBtn = new JButton();
		catalogManagerBtn = new JButton();
		createAspectBtn = new JButton();
		createCategoryBtn = new JButton();
		createCatalogBtn = new JButton();

		//======== this ========
		setOpaque(false);

		//---- headingWokspace ----
		headingWokspace.setText(Messages.getString(
				"PearReview.Moderator.aspects.workspace"));
		headingWokspace.setFont(new Font("Tahoma", Font.PLAIN, 14));

		//---- headingList ----
		headingList.setText(Messages.getString(
				"PearReview.Moderator.aspects.review_aspects"));
		headingList.setFont(new Font("Tahoma", Font.PLAIN, 14));

		//---- refreshBtn ----
		refreshBtn.setAction(refreshAction);
		refreshBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));

		//---- catalogManagerBtn ----
		catalogManagerBtn.setAction(openCatalogManager);
		catalogManagerBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));

		//---- createAspectBtn ----
		createAspectBtn.setAction(addAspectAction);
		createAspectBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));

		//---- createCategoryBtn ----
		createCategoryBtn.setAction(addCategoryAction);
		createCategoryBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));

		//---- createCatalogBtn ----
		createCatalogBtn.setAction(createCatalogAction);
		createCatalogBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		ArrayList<JComponent> workspaceList = new ArrayList<JComponent>();
		workspaceList.add(refreshBtn);
		workspaceList.add(catalogManagerBtn);
		calcPreferredSize(workspaceList);
		
		ArrayList<JComponent> reviewList = new ArrayList<JComponent>();
		reviewList.add(createAspectBtn);
		reviewList.add(createCategoryBtn);
		reviewList.add(createCatalogBtn);
		calcPreferredSize(reviewList);

		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup()
						.addComponent(headingWokspace, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(refreshBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(catalogManagerBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addGap(50)
					.addGroup(layout.createParallelGroup()
						.addComponent(createCatalogBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(headingList, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(createAspectBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(createCategoryBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addGap(40))
		);
		
		layout.setVerticalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup(
							GroupLayout.Alignment.BASELINE)
						.addComponent(headingWokspace, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(headingList, GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addGap(20)
					.addGroup(layout.createParallelGroup()
						.addGroup(layout.createSequentialGroup()
							.addComponent(refreshBtn)
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(catalogManagerBtn))
						.addGroup(layout.createSequentialGroup()
							.addComponent(createAspectBtn)
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(createCategoryBtn)
							.addPreferredGap(
									LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(createCatalogBtn)))
					.addContainerGap(20, Short.MAX_VALUE))
		);
	}
	
	private Dimension calcPreferredSize(ArrayList<JComponent> list) {
		Dimension biggestDim = new Dimension(1,1);
		for (JComponent comp: list) {
			Dimension currentDim = comp.getPreferredSize();
			if (currentDim.height > biggestDim.height)
				biggestDim.height = currentDim.height;
			if (currentDim.width > biggestDim.width)
				biggestDim.width = currentDim.width;
		}
		
		for (JComponent comp: list) {
			comp.setPreferredSize(biggestDim);
		}
		
		return biggestDim;
	}
}
